GMMMPEC_dc_ktr_FE <- function(x) { 

x<-as.matrix(x)
  

theta1 <- x[1:(K_A+1), 1]     
theta2 <- x[((K_A+1)+1):((nD+1)*(K_A+1) + (countries-1) + nD),1]  
betaf <- theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]
sigmaf <- theta2[(length(theta2)-nD+1):length(theta2)]
delta = x[((nD+1)*(K_A+1) + (countries-1) + nD + 1):((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal), 1]              
g = x[((nD+1)*(K_A+1) + (countries-1) + nD + numProdsTotal + 1):nrow(x), 1]  
g <- as.matrix(g) 
   

cong <- g - t(IV)%*%(delta - x_char[,1:(K_A+1)]%*%theta1)

expmu = matrix(0,nrow(x_char),ncol(v))

for (t in 1:T) { 
theta2_mat = rbind(as.matrix(theta2[1:(K_A+1)]), theta2[length(theta2)-nD+1]*as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))
expmu[marketStarts[t,1]:marketEnds[t,1],] = exp(x_char[marketStarts[t,1]:marketEnds[t,1],]%*%diag(as.numeric(theta2_mat))%*%v[1:(K+1),,t] + x_char[marketStarts[t,1]:marketEnds[t,1],]%*% rbind(matrix(0,K_A+1,1), as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))%*%matrix(1,1,nn))   
for (dd in 2:nD) {
theta2_mat = rbind(as.matrix(theta2[((dd-1)*(K_A+1)+1):(dd*(K_A+1))]), theta2[length(theta2)-nD+dd]*as.matrix(theta2[(nD*(K_A+1)+1):(length(theta2)-nD)]))   
expmu[marketStarts[t,1]:marketEnds[t,1],] = expmu[marketStarts[t,1]:marketEnds[t,1],] * exp(x_char[marketStarts[t,1]:marketEnds[t,1],]%*%diag(as.numeric(theta2_mat))%*%v[((dd-1)*(K+1)+1):(dd*(K+1)),,t]) 
}
}



expmeanval = as.matrix(exp(delta))
simEstShare<-ind_shnormMPEC(expmeanval,expmu, oo, sharesum, marketForProducts)
simShare<-simEstShare[,1:(ncol(simEstShare)-1)]
EstShare<-simEstShare[,ncol(simEstShare)]
EstShare<-as.matrix(EstShare)
share<-as.matrix(share) 

c<-c(rbind(EstShare - share, cong)) 
  
  
nx<-nrow(x)
ng<-nrow(g)
ooo<-matrix(1,1,K_A+1)
ooo2 <- matrix(1,1,nD)
  
# Evaluate the Gradients
dSdtheta2<-matrix(0,numProdsTotal,(nD*(K_A+1)))
dSddeltaDIAG<-matrix(0,numProdsTotal,prods)
dSddelta<-matrix(0,numProdsTotal,numProdsTotal)
dSdbetaf = matrix(0, numProdsTotal, countries-1)
dSdsigmaf = matrix(0, numProdsTotal, nD)
  

for (t in 1:T) {
  index<-marketStarts[t]:marketEnds[t]
  ooo1<-matrix(1,prodsMarket[t],1)
  index_start <- marketStarts[t]
  index_end <- marketEnds[t]
  
  dSddeltaDIAG_old <- matrix(0, length(index),prodsMarket[t])
  dSddeltaDIAG_temp = dc_1_cpp (simShare, index_start, index_end, dSddeltaDIAG_old, nn )
  dSddeltaDIAG[index,1:prodsMarket[t]] = dSddeltaDIAG_temp
  dSddelta[index,index] = dSddeltaDIAG[index,1:prodsMarket[t]]
  
  dSdtheta2 = dc_2_cpp_new(dSdtheta2, simShare, ooo, ooo1, v[,,t], x_char, nn, index_start, index_end, nD, K, K_A, t) 
  
  dSdbetaf_old <- matrix(0, length(index),countries-1)
  dSdbetaf[index,1:(countries-1)] = dSdbetaf_cpp(dSdbetaf_old , as.matrix(sigmaf), simShare, ooo, ooo1, v[,,t], x_char, nn, index_start, index_end, nD, K, K_A, t, countries)
  
  dSdsigmaf_old <- matrix(0, length(index), nD)
  dSdsigmaf[index,] = dSdsigmaf_cpp(dSdsigmaf_old, as.matrix(betaf), as.matrix(sigmaf), simShare, ooo, ooo1, ooo2, v[,,t], x_char, nn, index_start, index_end, nD, K, K_A, t, countries)
      
}



dc1<-cbind(matrix(0,numProdsTotal,K_A+1), dSdtheta2, dSdbetaf, dSdsigmaf, dSddelta, matrix(0,numProdsTotal,ng))
dc2 =cbind(t(IV)%*%x_char[,1:(K_A+1)], matrix(0,ng, nD*(K_A+1) + (countries-1) + nD), -t(IV), diag(ng))

dc = rbind(dc1, dc2)
dc = matrix(dc,nrow(dc),ncol(dc))

dc2<-dc[1,index_Jacobian[1,]]
for (i in 2:nrow(dc)) { 
dc2_temp<-dc[i,index_Jacobian[i,]]
dc2<-c(dc2,dc2_temp)
}


return(dc2)
  
}  